package org.gjy.m8.thread;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-31 21:39:20
 */
public class SemaphoreTest {

    @Test
    public void test1()  throws Exception{
        Semaphore semaphore = new Semaphore(0);

        ExecutorService pool = Executors.newFixedThreadPool(2);
        pool.execute(() -> {
            System.out.println("start 1");
            semaphore.release();
            System.out.println("end 1");
        });
        pool.execute(() -> {
            System.out.println("start 2");
            semaphore.release();
            System.out.println("end 2");
        });
        semaphore.acquire(2);
        System.out.println("main 1");
        pool.execute(() -> {
            System.out.println("start 3");
            semaphore.release();
            System.out.println("end 3");
        });
        pool.execute(() -> {
            System.out.println("start 4");
            semaphore.release();
            System.out.println("end 4");
        });
        semaphore.acquire(2);
        System.out.println("main 2");
        pool.shutdown();
    }
}
